using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Login : MonoBehaviour
{
    string user = "";
    string pwd = "";
    /// <summary>
    /// 只被调用一次,变量声明
    /// </summary>
    void Awake()
    {
        //NetWriter.SetMd5Key("");
    }

    /// <summary>
    /// 只被调用一次
    /// </summary>
    void Start()
    {
    }

    /// <summary>
    /// 每一帧调用
    /// </summary>
    void Update()
    {

    }

    /// <summary>
    /// 每一帧调用,处理Rigdibody时代替Update方法
    /// </summary>
    void FixedUpdate()
    {

    }

    /// <summary>
    /// 每一帧调用,Update方法全部执行完后执行
    /// </summary>
    void LateUpdate()
    {

    }

    /// <summary>
    /// 每一帧被调用多次,enabled=false时禁用
    /// </summary>
    void OnGUI()
    {
        NetWriter.SetMd5Key("qqqqqq");
        int cwidth = Screen.width / 2;
        int cheight = Screen.height / 2;
        var boxPos = new Rect(cwidth - 110, cheight - 160, 240, 120);

        GUI.Box(boxPos, "");
        GUI.Label(new Rect(cwidth - 100, cheight - 150, 100, 22), "User:");
        user = GUI.TextField(new Rect(cwidth - 10, cheight - 150, 120, 22), user, 20);
        GUI.Label(new Rect(cwidth - 100, cheight - 120, 100, 22), "Password:");
        pwd = GUI.PasswordField(new Rect(cwidth - 10, cheight - 120, 120, 22), pwd, '*', 20);

        if (GUI.Button(new Rect(cwidth - 100, cheight - 80, 80, 22), "Regist"))
        {
            //TODO:登录服务器分布式访问
            //NetWriter.SetUrl("http://pass.scutgame.com/", ResponseContentType.Json, true);
            //Debug.Log("Started to access login server, resp-type:" + NetWriter.ResponseContentType.ToString());

            //NetWriter.SetUrl("127.0.0.1:9001");
            //ActionParam ap = new ActionParam();
            //ap["passportID"] = user;
            //ap["password"] = pwd;
            //Net.Instance.Send(101, RegistCallback, ap);


            GameSetting.Instance.Pid = user;
            GameSetting.Instance.Password = pwd;


			string sign = MD5Utils.Encrypt("Handler=Regist&MobileType=0&IsCustom=true&Pid=" + user + "&Pwd=" + pwd, "qqqqqq");
			//string sign = "qqqqqq";
			//string res = HttpUtils.GetWebRequest("http://127.0.0.1:8801/Default.ashx", "Handler=Login&MobileType=0&Pid=Z10000&Pwd=111111&IMEI=&ScreenX=&ScreenY&RetailID=&sign=" + sign);
			string s = string.Format("http://www.aladdin.accountserver/Default.ashx?Handler=Regist&MobileType=0&IsCustom=true&Pid=" + user + "&Pwd=" + pwd + "&sign=" + sign);

            HttpGet.Instance.Get(s, onRegist,null);
			
        }

        if (GUI.Button(new Rect(cwidth, cheight - 80, 80, 22), "Login"))
        {
            //NetWriter.SetUrl("127.0.0.1:9001");

            //Debug.Log(NetWriter.GetUrl());
            GameSetting.Instance.Pid = user;
            GameSetting.Instance.Password = pwd;
            //Net.Instance.Send((int)ActionType.Login, LoginCallback, null);


            //string pwdm = "111111";// MD5Utils.Encrypt("111111", "", Encoding.UTF8);



            string sign = MD5Utils.Encrypt("Handler=Login&MobileType=0&Pid=" + user + "&Pwd=" + pwd, "qqqqqq");
            //string res = HttpUtils.GetWebRequest("http://127.0.0.1:8801/Default.ashx", "Handler=Login&MobileType=0&Pid=Z10000&Pwd=111111&IMEI=&ScreenX=&ScreenY&RetailID=&sign=" + sign);
            HttpGet.Instance.Get("http://www.aladdin.accountserver/Default.ashx?Handler=Login&MobileType=0&Pid=" + user + "&Pwd=" + pwd + "&IMEI=&ScreenX=&ScreenY&RetailID=1" + "&sign=" + sign, onLogin, null);
            string a = "";
        }

    }

    void onRegist(string result)
    {
        string res = result;

        var data = JsonConvert.DeserializeObject<RData>(res);


        NetWriter.SetUrl("127.0.0.1:9001");
        ActionParam ap = new ActionParam();
        ap["Token"] = data.Data.Token;
        ap["MobileType"] = 0;
        Net.Instance.Send((int)ActionType.Login, LoginCallback, ap);
    }

    void onLogin(string result)
    {
        string res = result;

        var data = JsonConvert.DeserializeObject<RData>(res);


        NetWriter.SetUrl("127.0.0.1:9001");
        ActionParam ap = new ActionParam();
        ap["Token"] = data.Data.Token;
        ap["MobileType"] = 0;
        Net.Instance.Send((int)ActionType.Login, LoginCallback, ap);
    }

    public class RData
    {
        public int StateCode { get; set; }

        public string StateDescription { get; set; }

        public string Vesion { get; set; }

        public string Handler { get; set; }

        public DT Data { get; set; }

        //        {
        //    "StateCode": 0,
        //    "StateDescription": "",
        //    "Vesion": "1.0",
        //    "Handler": "Login",
        //    "Data": 
        //    {
        //        "Token": "3074e807e73c4a899facb455aab1725e",
        //        "UserID": 1395265
        //    }
        //} 
    }

    public class DT
    {
        public string Token { get; set; }

        public string UserID { get; set; }

    }

    private void RegistCallback(ActionResult actionResult)
    {
        if (actionResult != null)
        {
            user = actionResult.Get<string>("passportID");
            pwd = actionResult.Get<string>("password");
        }
    }

    private void LoginCallback(ActionResult actionResult)
    {
        Debug.Log("AAAAAAAAAAAAAAAAuserId=" + NetWriter.SessionID);
        if (actionResult != null && actionResult.Get<int>("GuideID") == (int)ActionType.CreateRote)
        {
            //Application.LoadLevelAsync("RoleScene");
            SceneManager.LoadSceneAsync("RoleScene");
            return;
        }

        //ActionParam ap = new ActionParam();
        //ap["PageIndex"] = 1;
        //ap["PageSize"] = 20;
        //Net.Instance.Send(1001, OnRankCallback, ap);

        //Application.LoadLevelAsync("MainScene");
        SceneManager.LoadSceneAsync("MainScene");
    }

    private void OnRankCallback(ActionResult actionResult)
    {
        if (actionResult != null)
        {
            //string userName = actionResult.Get<string>("UserName");
            //int score = actionResult.Get<int>("Score");
            //var lst = actionResult.Get<List<FriendsServerData>>("list");
            Response1001Pack pack = actionResult.GetValue<Response1001Pack>();
            if (pack == null)
            {
                return;
            }
            var lst = pack.Items;
            string a = "";
        }
    }

    /// <summary>
    /// 不销毁对象,场景切换时对象依然存在.
    /// </summary>
    void DontDestroyOnLoad()
    {

    }
}

public class FriendsServerData
{
    public string UserName;
    public int Score;
}
